﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web.DynamicData;

namespace DD_Extensions
{
    public partial class HideableText_EditField : ChildChangingAwareFieldTemplate //System.Web.DynamicData.FieldTemplateUserControl
    {
        #region Event
        // added page init to hookup the event handler
        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // set field options
            this.SetFieldOptions();

            // add CSS from meta data
            this.ApplyCss();

            if (ParentColumns.Count > 0 && ParentControls.Count > 0)
            {
                foreach (var parent in ParentControls)
                {
                    if (parent.Value != null)
                        parent.Value.StateChanged += StateChanged;
                }
            }
        }

        // consume event
        protected void StateChanged(object sender, ChangingAwareEventArgs e)
        {
            if (e.ListItem != null)
                SetRequiredState(e.ListItem);
        }

        private void SetRequiredState(ListItem listItem)
        {
            var changeAware = Column.GetAttribute<ChangeAwareAttribute>();
            if (changeAware != null)
            {
                if (listItem.Text == changeAware.ValueWhenTrue)
                {
                    RequiredFieldValidator1.Enabled = true;
                    RequiredFieldValidator1.ErrorMessage = String.Format("The {0} field is required.", Column.DisplayName);
                    TextBox1.CssClass += " " + FieldTemplateExtensionMethods.RequiredCssClass;
                }
                else
                {
                    RequiredFieldValidator1.Enabled = false;
                    TextBox1.CssClass.Replace(" " + FieldTemplateExtensionMethods.RequiredCssClass, "");
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // set default state
            if (ParentColumns.Count > 0 && ParentControls.Count > 0)
            {
                var changeAware = Column.GetAttribute<ChangeAwareAttribute>();
                foreach (var parent in ParentControls)
                {
                    if (parent.Value != null && parent.Value.State != null)
                        SetRequiredState(parent.Value.State);
                }
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Column.MaxLength < 20)
                TextBox1.Columns = Column.MaxLength;

            TextBox1.ToolTip = Column.Description;

            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);

            // set validators to use image as error indicator
            this.SetValidatorsToImage();

            // check to see if column is read only in insert mode
            Column.DisableFieldTemplate(Mode, TextBox1);

            // apply mask
            TextBox1_MaskedEditExtender.SetupMask(Column);
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (Column.MaxLength > 0)
                TextBox1.MaxLength = Math.Max(FieldValueEditString.Length, Column.MaxLength);
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(TextBox1.Text.Trim());
        }

        public override Control DataControl
        {
            get { return TextBox1; }
        }
    }
}
